import 'package:flutter/material.dart';
import 'package:myflutter/ws/save/SharedPreferencesHelper.dart';
import 'package:myflutter/ws/theme/MyColors.dart';

import 'gamePage.dart';

class NamePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return NamePageState();
  }

}



class NamePageState extends State<StatefulWidget> {

  String _username = "取个名字";
  bool _isButtonEnabled = false;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("$_username"),),
      body:
        Center(
          child: Column(
            children: [
              SizedBox(height: 60),
              Container(
                width: 200,
                height: 100,
                child: TextField(
                  decoration: InputDecoration(
                      hintText: "昵称"
                  ),
                  onChanged: (text) {
                    setState(() {
                      _username = text;
                      _isButtonEnabled = true;
                      if (text.isEmpty) {
                        _username = "取个名字";
                        _isButtonEnabled = false;
                      }
                    });
                  },
                ),
              ),
              // SizedBox(height: 40),
              Container(
                height: 40,
                width: 200,
                child: ElevatedButton(
                  style: ButtonStyle(
                    backgroundColor: MyColors().getTimeColor(context),
                  ),
                  onPressed: _isButtonEnabled ? () {
                    SharedPreferencesHelper.saveString("name", "$_username");
                    Navigator.pushAndRemoveUntil(
                      context,
                      MaterialPageRoute(
                        builder: (context) => GamePage(),
                        settings: RouteSettings(arguments: {'username': '$_username'}),
                      ),
                          (route) => false, // 移除之前的所有页面
                    );
                  } : null,
                  child: Text("继续", style: TextStyle(color: MyColors().getTimeTextColor(context)),),

                ),
              ),
            ],
          ),
        )
      );
  }

}